﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using siqi.Interfaces;
using siqi.Interfaces.Data;

namespace siqi.Logic
{
    [EC.Controller]
    public class SiqiServer:Interfaces.ISiqiServer
    {

        public SiqiServer()
        {
            Users = new Dictionary<string, Interfaces.IUserAgent>();
            Rooms = new List<Interfaces.IRoom>();
            mLoginDispather = new Dispatch.Dispatcher();
            LoginHandler = new siqi.Interfaces.Data.LoginHanlder();
            
        }

        private Dispatch.Dispatcher mLoginDispather;

        public ILoginHandler LoginHandler
        {
            get;
            set;
        }

        public IDictionary<string, Interfaces.IUserAgent> Users { get;private set; }

        public IList<Interfaces.IRoom> Rooms
        {
            get;
            private set;
        }

        public void AddUser(IUserAgent agent)
        {
            Users[agent.ID] = agent;
        }

        public IUserAgent GetUser(string id)
        {
            IUserAgent result=null;
            Users.TryGetValue(id, out result);
            return result;
                
        }

        public void Add(IRoom room)
        {
            throw new NotImplementedException();
        }

        public void Login(EC.ISession session, Protocol.Login e)
        {
            mLoginDispather.Add(new Dispatch.LoginJob { Login=e, Session= session, SiqiServer= this });
        }

        public void SelectRoom(EC.ISession session, Protocol.SelectRoom e)
        {

        }

        public void SelectDesk(EC.ISession session, Protocol.SelectDesk e)
        {

        }

        public void ListRoom(EC.ISession session, Protocol.ListRoom e)
        {

        }

        public void Talk(EC.ISession session, Protocol.Talk e)
        {

        }


        public EC.IApplication Application
        {
            get
            {
                return AppModel.Application;
            }
            set
            {
            }
        }

        public void Send(Protocol.IMessage message, params EC.ISession[] session)
        {
            for(int i=0;i<session.Length ;i++)
                Application.Server.Send(message, session[i].Channel);
        }
    }
}
